//
//  ESTCarBrandTableViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/18.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTCarBrandTableViewController.h"
#import "ESTCarBrandModel.h"
#import "ESTCarBrandSeriesConfigSectionHeaderView.h"
#import "ESTCarModelTableViewController.h"

@interface ESTCarBrandTableViewController ()
/**
 *  车辆品牌的数据源
 */
@property(nonatomic, copy) NSArray<ESTCarBrandContentModel *> *brandModelArray;
/**
 *  总数据源
 */
@property(nonatomic, strong) NSMutableArray<NSArray<ESTCarBrandSeriesModel *> *> *dataArray;
/**
 *  请求完成的计数器
 */
@property(nonatomic, assign) NSInteger requestCount;
/**
 *  控制组头展开收缩的数组
 */
@property(nonatomic, strong) NSMutableArray<NSNumber *> *sectionNumber;
@end

@implementation ESTCarBrandTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = [@"选择品牌、车系" S];
    self.tableView.tableFooterView = [[TYZView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self acquiredCarBrand];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)acquiredCarBrand {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTCarBrandModel enquiriesCarBrandWithDLR_ID:self.DLR_ID success:^(NSArray<ESTCarBrandContentModel *> *modelArray) {
        weakSelf.brandModelArray = modelArray;
    } failure:^(NSString *error) {
        [TYZLoadingView showError:NetworkUnusual];
    }];
}
- (void)acquiredCarSeriesWithBrand_ID:(NSString *)brand_id {
    WEAKSELF
    [ESTCarBrandModel enquiriesCarSeriesWithDLR_ID:self.DLR_ID brand_id:brand_id success:^(NSArray<ESTCarBrandSeriesModel *> *modelArray) {
        [weakSelf.dataArray addObject:modelArray];
        weakSelf.requestCount++;
    } failure:^(NSString *error) {
        weakSelf.requestCount++;
    }];
}
- (void)handleSeriesAllModelArray {
    // 按照原来的顺序排列
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.brandModelArray.count];
    [self.brandModelArray enumerateObjectsUsingBlock:^(ESTCarBrandContentModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (NSArray<ESTCarBrandSeriesModel *> *array in self.dataArray) {
            if ([obj.CAR_BRAND_ID isEqualToString:array.firstObject.CAR_BRAND_ID]) {
                [arrM addObject:array];
                break;
            }
        }
        [self.sectionNumber addObject:@(NO)];
    }];
    self.dataArray = arrM;
    if (arrM.count > 0) {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.tableView removeNoDataImage];
        [self.tableView reloadData];
    } else {
        WEAKSELF
        [self.tableView noDataWithClick:^{
            [weakSelf acquiredCarBrand];
        }];
    }
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.sectionNumber[section] isEqualToNumber:@(YES)]) {
        return 0;
    }
    return self.dataArray[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ESTCarBrandSeriesConfigSectionHeaderView *headerView = [ESTCarBrandSeriesConfigSectionHeaderView creatCarBrandSeriesConfigSectionHeaderView:tableView];
    headerView.leftTitle = self.dataArray[section].firstObject.CAR_BRAND_CN;
    headerView.rightValue = self.dataArray[section].count;
    WEAKSELF
    [headerView setDidClickCoverBlock:^{
        BOOL value = [weakSelf.sectionNumber[section] integerValue];
        [weakSelf.sectionNumber replaceObjectAtIndex:section withObject:@(!value)];
        [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    }];
    return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTCarBrandTableViewController"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTCarBrandTableViewController"];
    }
    cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row].CAR_SERIES_CN;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.enterModel) {
        ESTCarModelTableViewController *controller = [[ESTCarModelTableViewController alloc] init];
        controller.DLR_ID = self.DLR_ID;
        controller.model = self.dataArray[indexPath.section][indexPath.row];
        WEAKSELF
        [controller setSelectedCarModelBlock:^(NSString *modelName, NSString *modelID) {
            if (weakSelf.selectedSeriesBlock) {
                weakSelf.selectedSeriesBlock(modelName, modelID);
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }];
        [self.navigationController pushViewController:controller animated:YES];
    } else {
        if (self.selectedSeriesBlock) {
            ESTCarBrandSeriesModel *model = self.dataArray[indexPath.section][indexPath.row];
            self.selectedSeriesBlock(model.CAR_SERIES_CN, model.CAR_SERIES_ID);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma mark - setter & getter
- (void)setRequestCount:(NSInteger)requestCount {
    if (requestCount == self.brandModelArray.count) {
        [TYZLoadingView hideHUD];
        [self handleSeriesAllModelArray];
        requestCount = 0;
    }
    _requestCount = requestCount;
}
- (void)setBrandModelArray:(NSArray<ESTCarBrandContentModel *> *)brandModelArray {
    _brandModelArray = brandModelArray;
    [brandModelArray enumerateObjectsUsingBlock:^(ESTCarBrandContentModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self acquiredCarSeriesWithBrand_ID:obj.CAR_BRAND_ID];
    }];
}
- (NSMutableArray<NSArray<ESTCarBrandSeriesModel *> *> *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSArray<NSNumber *> *)sectionNumber {
    if (_sectionNumber == nil) {
        _sectionNumber = [NSMutableArray array];
    }
    return _sectionNumber;
}
@end
